package com.ybwh.springboot2.common.conf;


import com.ybwh.springboot2.activiti.listener.AssignmentTaskListener;
import com.ybwh.springboot2.activiti.listener.MyUserTaskParseHandler;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * activiti5 没有 springboot2 的starter ,故要手动引入
 */
@Slf4j
@Configuration
public class ActivitiConfig {

    @Autowired
    DataSource dataSource;
    @Autowired
    PlatformTransactionManager transactionManager;

    @Bean
    public SpringProcessEngineConfiguration processEngineConfiguration() throws SQLException {
        SpringProcessEngineConfiguration config = new SpringProcessEngineConfiguration();
        config.setDatabaseType(dataSource.getConnection().getMetaData().getDatabaseProductName().toLowerCase());
        config.setDataSource(dataSource);
        config.setTransactionManager(transactionManager);
        if (StringUtils.isNotBlank(dataSource.getConnection().getCatalog())) {
            config.setDatabaseCatalog(dataSource.getConnection().getCatalog());
        }

//        config.setDatabaseSchemaUpdate("false");
        config.setDatabaseSchemaUpdate("true");
        config.setProcessDefinitionCacheLimit(50);
        config.setIdBlockSize(5000);
        // 自定义行为
//            config.setActivityBehaviorFactory(new CustomBehaviorFactory(this.customBehaviorServiceWrapper));
        log.info("数据源类型为:{},Catalog为:{}", config.getDatabaseType(), config.getDatabaseCatalog());

        //关闭全局事件监听器
//        config.setEnableEventDispatcher(false);
        // 全局监听器
        config.setEventListeners(Arrays.asList(new ActivitiEventListener() {

            @Override
            public void onEvent(ActivitiEvent event) {
                log.info("-----------------eventName:" + event.getType().name());
            }

            @Override
            public boolean isFailOnException() {
                return false;
            }
        }));


        Map<String, List<ActivitiEventListener>> typedListeners = new HashMap<>();
        typedListeners.put(ActivitiEventType.PROCESS_STARTED.name(), Arrays.asList(new ActivitiEventListener() {

            @Override
            public void onEvent(ActivitiEvent event) {
                log.info("-----------------eventName:" + event.getType().name());
            }

            @Override
            public boolean isFailOnException() {
                return false;
            }
        }));

        config.setTypedEventListeners(typedListeners);

        // 自定义对象解析器
        config.setCustomDefaultBpmnParseHandlers(Arrays.asList(new MyUserTaskParseHandler(Arrays.asList(new AssignmentTaskListener()))));


        // 自定义拦截器
//        List<CommandInterceptor> commandInterceptors = new ArrayList<>();
//        config.setCommandInterceptors(commandInterceptors);

        config.setEnableDatabaseEventLogging(true);

        return config;

    }


}
